home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 32 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: nyx10.cs.du.edu!not-for-mail
  2. From: cwyles@nyx10.cs.du.edu (Carl Wyles)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Faster FILE subroutines in standard C?
  5. Date: 5 Jan 1996 09:13:22 -0700
  6. Organization: University of Denver, Math/CS Dept.
  7. Message-ID: <4cjin2$1bp@nyx10.cs.du.edu>
  8. References: <4cfm7c$dot@sunburst.ccs.yorku.ca>
  9. NNTP-Posting-Host: nyx10.cs.du.edu
  10. X-Newsreader: NN version 6.5.0 #3 (NOV)
  11.  
  12. ken@yorku.ca (Kenneth Kafieh) writes:
  13.  
  14. >I had to resort to drastic mesures inorder to put this message here.
  15.  
  16. >I found that I was able to get 3-4 time faster performance by using good 
  17. >old ANSI C file accessing routines (like  fgets, fseek, ftell, ...etc.)
  18. >instead of C++ streams (like ifstream::getline, fstream::seekg, ...etc.)
  19. >I wrote the program originally in C++ (a simple little phonebook for my
  20. >own use) and it took about 9 seconds to scan a 100Kb file for a 3 byte
  21. >string!!!  Fed up, I rewrote it in C and voila! Now it takes 2 secs!
  22. >I'm using Borland C++ v2.0.
  23.  
  24. >Does that sound right?  Maybe my code was inefficiently written?
  25. >I know C++ is generally slower than C but I didn't think that 
  26. >file stuff would be too.   What did I do wrong? Am I missing
  27. >something.  Maybe I'll post the code too, later. Has anyone
  28. >else noticed this too?
  29.  
  30. I think that is about right. C++ file routines can be a little slow...
  31.  
  32. If the record length is more than the length of the data you are 
  33. searching for, I would suggest putting a sorted index on the file. 
  34.  
  35. I have a fortune cookie program I wrote that currently is 1.4Mb long and 
  36. has 10,000+ fortunes. I implemented a index on it, and made a file about 
  37. 41k long. It took about 5 minutes to create the index from scratch, but 
  38. much less than a second to pick any particular fortune. Eg almost as soon 
  39. as my finger clears the Enter key it prints a fortune.
  40.  
  41. Use a binary search on the index, get a pointer to the data file, and 
  42. bingo you will get the record in question.
  43.  
  44. Otherwise; Sort the data and use a binary search on it.
  45.  
  46. Carl
  47. -- 
  48. >>>>> All flames sent to Demon Dragon familiar for appropriate response <<<<<
  49. cwyles@nyx10.cs.du.edu | My words are the amalgamation of my experiences only
  50. Good advice is ignored. Ok advice is used. Bad advice is flamed forever.
  51.